package com.yueya.common.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {

	public static String yearFormat = "yyyy";
	public static String monthFormat = "yyyy-MM";
	public static String dateFormat = "yyyy-MM-dd";
	public static String timeFormat = "yyyy-MM-dd HH:mm:ss";
	public static String millisecondFormat = "yyyy-MM-dd HH:mm:ss.SSS";

	public static String YEAR = "year";
	public static String MONTH = "month";
	public static String DAY = "day";
	public static String HOUR = "hour";

	public static String getDate(String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		Date currentTime = new Date();
		return formatter.format(currentTime);
	}

	public static String getDate(Date date, String format) {
		SimpleDateFormat formatter = new SimpleDateFormat(format);
		return formatter.format(date);
	}

	public static long between(String type, String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, Locale.CHINA);
		Date now1 = new Date();
		String now = sdf.format(now1);
		Date date = null;
		try {
			now1 = sdf.parse(now);
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long l = now1.getTime() - date.getTime();

		if (YEAR.equals(type)) {
			long day = l / (24 * 60 * 60 * 1000);
			long mon = day / 30;
			long year = mon / 12;
			// System.out.println("相差" + year + "年");
			return year;
		} else if (MONTH.equals(type)) {
			long day = l / (24 * 60 * 60 * 1000);
			long mon = day / 30;
			// System.out.println("相差" + mon + "月");
			return mon;
		} else if (DAY.equals(type)) {
			long day = l / (24 * 60 * 60 * 1000);
			// System.out.println("相差" + day + "天");
			return day;
		} else if (HOUR.equals(type)) {
			long day = l / (24 * 60 * 60 * 1000);
			long hour = day * 24;
			// System.out.println("相差" + hour + "小时");
			return hour;
		}

		return 0;

	}

	public static String getDateByYear(int year) {
		Calendar calendar = Calendar.getInstance();
		Date date = new Date(System.currentTimeMillis());
		calendar.setTime(date);
		calendar.add(Calendar.YEAR, -year);
		date = calendar.getTime();

		// System.out.println(getDate(date, timeFormat));
		return getDate(date, timeFormat);
	}

	public static String betweenBeforeTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, Locale.CHINA);
		Date now = new Date();
		Date date = null;
		try {
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
			return "";
		}
		long mss = now.getTime() - date.getTime();
		if (0 > mss)
			return "刚刚";

		long days = mss / (1000 * 60 * 60 * 24);
		long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
		long minutes = (mss % (1000 * 60 * 60)) / (1000 * 60);
		long seconds = (mss % (1000 * 60)) / 1000;
		String ret = "";
		if (days > 0)
			ret = days + "天前";
		else if (hours > 0)
			ret = hours + "小时前";// + (minutes>0?(minutes + "分钟前"):"前")
		else if (minutes > 0)
			ret = minutes + "分钟前";
		else if (seconds > 0)
			ret = seconds + "秒前";
		return ret;
	}

	public static String formatTime(String time) {
		SimpleDateFormat sdf = new SimpleDateFormat(timeFormat, Locale.CHINA);
		Date now1 = new Date();
		String now = sdf.format(now1);
		Date date = null;
		try {
			now1 = sdf.parse(now);
			date = sdf.parse(time);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long mss = now1.getTime() - date.getTime();
		long days = mss / (1000 * 60 * 60 * 24);

		String ret = "";
		if (days > 1) {
			ret = time.substring(0, time.indexOf(" ")).replace("-", "/");
		} else if (days == 1) {

			Calendar cal = Calendar.getInstance();
			cal.add(Calendar.DATE, -1);
			Date yesday = cal.getTime();
			String yesdaystr = new SimpleDateFormat(dateFormat).format(yesday);
			if (yesdaystr.equals(time.substring(0, time.indexOf(" ")))) {
				long hours = (mss % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60);
				System.out.println("hours" + hours);
				String[] times = time.substring(time.indexOf(" ") + 1, time.length()).split(":");
				ret = "昨天 " + Integer.parseInt(times[0]) + ":" + times[1];
			} else {
				ret = time.substring(0, time.indexOf(" ")).replace("-", "/");
			}
		} else if (days == 0) {
			String[] times = time.substring(time.indexOf(" ") + 1, time.length()).split(":");
			if (getDate(dateFormat).equals(time.substring(0, time.indexOf(" ")))) {
				int hours = Integer.parseInt(times[0]) > 12 ? Integer.parseInt(times[0]) - 12
						: Integer.parseInt(times[0]);
				ret = (Integer.parseInt(times[0]) > 12 ? "下午" : "上午") + " " + hours + ":" + times[1];
			} else {
				ret = "昨天 " + Integer.parseInt(times[0]) + ":" + times[1];
			}
		} else {
			ret = "未来";
		}
		return ret;
	}

	/**
	 * 获取当前时间的后一天时间
	 * 
	 * @param cl
	 * @return
	 */
	public static String getAfterDay(String dateStr) {
		// 使用roll方法进行回滚到后一天的时间
		// cl.roll(Calendar.DATE, 1);
		// 使用set方法直接设置时间值

		String[] dateStrs = dateStr.split("-");
		Calendar cl = Calendar.getInstance();
		int y = Integer.valueOf(dateStrs[0]);
		int m = Integer.valueOf(dateStrs[1]);
		int d = Integer.valueOf(dateStrs[2]);
		cl.set(y, m - 1, d);

		int day = cl.get(Calendar.DATE);
		cl.set(Calendar.DATE, day + 1);

		int yy = cl.get(Calendar.YEAR);
		int mm = cl.get(Calendar.MONTH) + 1;
		String MM = mm < 10 ? "0" + mm : String.valueOf(mm);
		String dd = cl.get(Calendar.DATE) < 10 ? "0" + cl.get(Calendar.DATE) : String.valueOf(cl.get(Calendar.DATE));
		//System.out.println(yy + "-" + MM + "-" + dd);
		return yy + "-" + MM + "-" + dd;
	}

	public static void main(String args[]) {
		// Long l = System.currentTimeMillis();
		// System.out.println(l*2);
		// 1473241574668
		// 1473242440145
		// 1473242454801
		// 14733898993875130
		// between(YEAR,"2016-10-20 00:00:00");
		// between(DateUtil.HOUR, getDateByYear(1)) ;
		// System.out.println(formatTime("2017-03-22 16:43:20"));
		System.out.println(getAfterDay("2017-02-28"));
	}

}
